package com.gong.gongcloudpicture1.common;


import com.gong.gongcloudpicture1.exception.ErrorCode;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;


/**
 * 全局响应封装类
 *
 * @param <T>
 */
@Data
public class BaseResponse<T> implements Serializable {

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 信息
     */
    private String message;
    /**
     * 结构体
     */
    private T data;


    public BaseResponse() {
        this.code = 0;
        this.message = "success";
        this.data = null;
    }


    public BaseResponse(T data) {
        this.data = data;
        this.code = 0;
        this.message = "success";
    }

    public BaseResponse(String message, T data) {
        this.data = data;
        this.code = 0;
        this.message = message;
    }

    public BaseResponse(int code, String message) {
        this.data = null;
        this.code = code;
        this.message = message;
    }

    public BaseResponse(int code, String message, T data) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(0);
        response.setMessage("success");
        response.setData(null);
        return response;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(0);
        response.setMessage("success");
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> success(String msg, T data) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(200);
        response.setMessage(msg);
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> success(ErrorCode errorCode, T data) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(errorCode.getCode());
        response.setMessage(errorCode.getMessage());
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> success(ErrorCode errorCode) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(errorCode.getCode());
        response.setMessage(errorCode.getMessage());
        response.setData(null);
        return response;
    }


    public static <T> BaseResponse<T> success(int code, String msg, T data) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(code);
        response.setMessage(msg);
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> success(int code, String msg) {
        return error(code, msg);
    }


    public static  <T> BaseResponse<T> error(int code, String msg) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(code);
        response.setMessage(msg);
        response.setData(null);
        return response;
    }


    public static <T> BaseResponse<T> error(String msg) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(500);
        response.setMessage(msg);
        response.setData(null);
        return response;
    }


    public static <T> BaseResponse<T> error(ErrorCode errorCode) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(errorCode.getCode());
        response.setMessage(errorCode.getMessage());
        response.setData(null);
        return response;
    }


    public static <T> BaseResponse<T> error(ErrorCode errorCode, String msg) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(errorCode.getCode());
        response.setMessage(StringUtils.isNotBlank(msg) ? errorCode.getMessage() : msg);
        response.setData(null);
        return response;
    }


}
